package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.ConsignmentItem;
import labox.innovation.gameserver.model.ConsignmentManager;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.ConsignmentWarehouse;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ConsignmentUIPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * @author ljsh
 * @date 2010-5-5
 * @version 1.0
 * @description  从寄售仓库中取回物品
 */
public final class FetchItemConsignment extends L2GameClientPacket {
	
	private static Logger _log=LoggerFactory.getLogger(RequestItemShelve.class);
	private static final String _C_FETCHITEMCONSIGNMENT = "_C_FetchItemConsignment";

	private static final String process = "Fetch item from consignment warehouse";

	/**
	 * 物品ID
	 */
	private long itemid;

	@Override
	public String getType() {
		return _C_FETCHITEMCONSIGNMENT;
	}

	@Override
	protected void readImpl() {
		itemid = readLong();
	}

	@Override
	protected void runImpl() {
		FuzePcInstance active = getClient().getActiveChar();
		if (active == null)
			return;
		ConsignmentWarehouse house = ConsignmentManager.getConsignmentWarehouse(active.getObjectId());
		if (house == null) {
			return;
		}
		ConsignmentItem item = house.getConsignmentItem(itemid);
		if (item == null) {
			active.sendPacket(new SystemMessage(SystemMessageId.THIS_KIND_ITEM_CANNOT_FOUND_IN_THE_WAREHOUSE));
			return;
		}
		L2ItemInstance _item = item.getItem();
		if (_item == null) {
			return;
		}
		if (active.getInventory().validateCapacity(_item)) {
			active.getInventory().addItem(process, _item, active, null);
			_log.info("物品加入背包："+_item.getName()+"("+_item.getObjectId()+"),"+_item.getCount()+";从寄售仓库", active.getObjectId(), active.getName());
			house.removeConsignmentItem(item);
			_log.info("取回寄售物品："+_item.getName()+"("+_item.getObjectId()+"),"+_item.getCount()+";从寄售仓库", _item.getObjectId(), _item.getName());
			active.sendPacket(new ConsignmentUIPacket(active));
		}
	}

}
